package com.controller;

import com.domain.User;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class registerController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping(value = "/register") //注册地址
    public String register() {
        return "comm/register";
    }

    @PostMapping(value = "/toRegister") //提交注册信息地址
    public String toRegister(User registerUser, Model model) {
        try {
            int ok = userMapper.insertUser(registerUser);
            if (ok > 0) { //成功返回1
                model.addAttribute("msg", "注册成功，请登录！");
                return "client/login";
            } else {
                model.addAttribute("error", "注册失败，服务器出错请联系管理员！");
            }
        } catch (Exception e) {
            model.addAttribute("error", "注册失败，已存在用户：" + registerUser.getUserName());
        }
        return "/register";
    }

}
